package com.ws.ssmzongjie3.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.ssmzongjie3.bean.*;
import com.ws.ssmzongjie3.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/luyou")
public class QianTaiController {
    @Autowired
    private ColumnService columnService;
    @Autowired
    private RouteService routeService;
    @Autowired
    private CarouselService carouselService;
    @Autowired
    LinksService linksService;
    @Autowired
    private UserLikesService userLikesService;
    @Autowired
    private CollectionsService collectionsService;

    @RequestMapping("/sousuo")
    public String suosuo(Model model, Route route,ProVo proVo){
        System.out.println("links:"+route);

        Route route1 = new Route();
        route1.setRecommend("0");
        List<Column> columns = columnService.listColumn(new Column());
        List<Route> routes1 = routeService.listRoute(route1);

        model.addAttribute("routes1",routes1);
        model.addAttribute("columns",columns);
        PageHelper pageHelper = new PageHelper();//分页帮助类
        pageHelper.startPage(proVo.getCurrentPage(),proVo.getPageSize());//分页   每页条数
        route.setIs_delete(0);
        List<Route> routes = routeService.listRoute(route);

        PageInfo pageInfo = new PageInfo(routes);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("route1",route);
        model.addAttribute("route",routes);

        return "/WEB-INF/qiantai/route_list";
    }
    @RequestMapping("/xiangqing")
    public String xiangqing(Model model, Route route,HttpSession session){
        Collections collections2 = new Collections();
        collections2.setRoute_id(route.getRoute_id());
        UserLikes userLikess = new UserLikes();
        userLikess.setRoute_id(route.getRoute_id());
        List<UserLikes> userLikes2 = userLikesService.listUserLike(userLikess);//该线路点赞数
        int i = collectionsService.countCollectionByRouteId(route.getRoute_id());
        List<Route> routes = routeService.listRouteWithAttachments(route);
        Route route4 = new Route();
        for (Route route1 : routes) {
            route4 = route1;
        }

        Route routeById = routeService.getRouteById(route);
        List<Route> route1 = routeService.listRouteWithBusinessAndColumn(routeById);

        Route route3 = new Route();
        for (Route route2 : route1) {
            route3 = route2;
        }
        model.addAttribute("routeById",route3);
        model.addAttribute("likenum",userLikes2.size());
        model.addAttribute("collectionnum",i);
        model.addAttribute("route",route4);
        UserLikes userLikes = new UserLikes();
        userLikes.setRoute_id(route3.getRoute_id());
        Collections collections = new Collections();
        collections.setRoute_id(route3.getRoute_id());

        User userId = null;
        if (session.getAttribute("user")!=null){
            userId = (User) session.getAttribute("user");
            userLikes.setUser_id(userId.getUser_id());
        }

        if (userId != null) {
            collections.setUser_id(userId.getUser_id());
        }

        model.addAttribute("routes",routes);

        if (userId != null){
            List<UserLikes> userLikes1 = userLikesService.listUserLike(userLikes);
            List<Collections> collections1 = collectionsService.listCollectionByUserId(collections);

            if (!userLikes1.isEmpty()){//判断点赞状态
                model.addAttribute("like","true");
            }else {
                model.addAttribute("like","false");
            }

            if (!collections1.isEmpty()){//判断收藏状态
                model.addAttribute("collection","true");
            }else {
                model.addAttribute("collection","false");
            }
        }


        return "/WEB-INF/qiantai/route_detail";
    }
    @RequestMapping("/shouye")
    public String shouye(Model model, HttpSession session, Column column,Route route,Carousel carousel){
        List<Column> columns = columnService.listColumn(column);
        List<Route> routes = routeService.listRouteWithBusinessAndColumn(route);
        List<Carousel> carousels = carouselService.listCarousel(carousel);
        List<Links> links = linksService.listLinks(new Links());
        session.setAttribute("carousels",carousels);
        session.setAttribute("columns",columns);
        model.addAttribute("routes",routes);
        model.addAttribute("links",links);

        return "/WEB-INF/qiantai/index";
    }

    @RequestMapping("/login")
    public String login(){
        return "/login";
    }
    @RequestMapping("/zhuce")
    public String zhuce(){
        return "/register";
    }

    @RequestMapping("/luxianxiangqing")
    public String luxianxiangqing(){
        return "/WEB-INF/qiantai/route_detail";
    }

    public void setRouteService(RouteService routeService) {
        this.routeService = routeService;
    }
}
